GBML manual / Grids

grid_shuffle_columns

Mixes up the order of the columns of target, in place. Each column moves as a whole, so the cells of a column stay together and in the same order top to bottom; only which column comes first, second and so on changes. It makes no copy.

Syntax

grid_shuffle_columns(target)

Arguments

NameType
targetgrid

Returns

Nothing.

Description

Mixes up the order of the columns of target, a grid. Each column moves as a whole, so the cells of a column stay together and stay in the same order from top to bottom. Only which column comes first, second, third and so on changes.

Before          After grid_shuffle_columns(board)
1  2  3          3  1  2
4  5  6          6  4  5

To mix up every cell on its own instead, use grid_shuffle. To move whole rows, use grid_shuffle_rows.

It changes the grid itself

grid_shuffle_columns moves the values that are already in target. It does not make a new grid, and it needs no extra memory. The old order of the columns is gone afterwards. If you want to keep it, make a copy with grid_copy first. The grid keeps its width and height.

Any kind of value can be shuffled: numbers, text, structs, references. Nothing is compared, so there is no rule about what the grid holds.

Where the random numbers come from

A shuffle picks its random numbers from the same place roll_int does: one stream of random numbers for the whole game. There is no seed to give the shuffle itself. What follows from that:

Every possible order of the columns can come up. It is very slightly more likely to land on some than others, because a random number is cut down to fit a small range, but the difference is far too small for a game to show.

The grid has to know its size

To find a column, GBML needs to know how many cells are in a row. A grid only knows its width and height where it was made (grid_create(8, 6, 0), /// @capacity 8x6, or grid<int, 8, 6>). A grid with no size written like that, or a script's grid parameter, has none to give, and the call is refused, the same as grid_width is. In a script, grid_shuffle still works on the whole grid.

What happens in the awkward cases

You do thisWhat happens
shuffle a grid with one columnnothing changes, and no random numbers are used
shuffle columns that are the same as each otherworks as normal. You cannot tell which is which
shuffle and hope a column movesit may stay where it was. That can happen with random order
give it a list or an arrayrefused when checked
use it where a value is neededrefused when checked. It gives nothing back

What it costs

Nothing is set aside: the columns are swapped where they stand, one column swap (a swap of every cell in the two columns) for each column after the first. No timing was measured.

Example

var board = grid_create(3, 2, 0);
board[# 0, 0] = 1;
board[# 1, 0] = 2;
board[# 2, 0] = 3;
board[# 0, 1] = 4;
board[# 1, 1] = 5;
board[# 2, 1] = 6;
grid_shuffle_columns(board);    // the three columns in a new order, each column unchanged

On the GBA

A Fisher-Yates shuffle over the column numbers, where swapping two columns swaps their height cells one by one. A Fisher-Yates shuffle done in place: it makes no copy and needs no extra memory. For each place from the last down to the second, it swaps that place with one picked at random from the places up to and including it, so every order can come up. The pick comes from gbml::roll_int, the one stream the roll_ functions use, so a shuffle follows the same rules: the same ROM shuffles the same way every time it is switched on, roll_reseed changes that, and roll_reseed with the same number just before a shuffle gives the same shuffle again. A shuffle uses up rolls from that stream (one for each column after the first), so it moves every later roll along. Nothing is compared, so any kind of item can be shuffled. It needs the grid's width and height, which the checker reads from the declaration and writes into the C++ as plain numbers; a grid with none, such as a script's parameter, is refused.

See also: grid_shufflegrid_shuffle_rowsgrid_sortlist_shufflegrid_widthgrid_heightgrid_copyroll_introll_reseedGridsData containers